import React from 'react'
import styles from './Line.css'
import Highcharts from 'highcharts-release'
import R from 'ramda'
// import theme from './theme.js'

type Props = {
  xData: Array,
  chartData: Array,
  title: String,
  yTitle: String
}
class Line extends React.Component {
  props: Props

  componentDidMount () {
    this.draw()
  }

  componentDidUpdate (nextProps) {
    if (!R.equals(nextProps, this.props)) {
      this.draw()
    }
  }

  draw () {
    const container = this.refs.container
    let innerChart = Highcharts.chart(container, {
      chart: {
        backgroundColor: 'transparent'
      },
      title: {
        text: this.props.title,
        floating: true,
        align: 'left',
        style: {
          fontSize: '16px'
        }
      },
      credits:{
        enabled: false
      },
      legend: {
        enabled: false
      },
      xAxis: {
        categories: this.props.xData
      },
      yAxis: {
        title: {
          text: this.props.yTitle
        },
        plotLines: [{
          value: 0,
          width: 1,
          color: '#808080'
        }]
      },
      plotOptions: {
        series: {
          dataLabels: {
            enabled: true,
            formatter: function () {
              return this.point.name + '(' + this.y + ')'
            }
          }
        }
      },
      series: this.props.chartData
    })
    setTimeout(() => {
      if (innerChart) {
        innerChart.reflow()
      }
    }, 5)
  }

  render () {
    return (
      <div ref='container' className={styles['container']} />
    )
  }
}
export default Line
